package com.cl.edu.service.teacher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.edu.dao.MessageDAO;
import com.cl.edu.entity.Message;

/**
 * 站内消息操作
 * @author Administrator
 *
 */ 
@Service
@Transactional
public class MessageManager {
	@Autowired
	private MessageDAO messageDAO;
	
	/**
	 * 分页查询给用户的站内消息 
	 * @param page
	 * @param to
	 * @return
	 */
	public Page<Message> getMessages(Page<Message> page,Long to) {
		StringBuffer hql = new StringBuffer("from Message model");
		hql.append(" where model.to=").append(to);
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = messageDAO.findPage(page, hql.toString());
		return page;
	}
	
	/**
	 * 查询给用户的站内最新未读消息 
	 * @param page
	 * @param to
	 * @return
	 */
	public Page<Message> getNoReadMessages(Page<Message> page,Long to) {
		StringBuffer hql = new StringBuffer("from Message model");
		hql.append(" where model.to=").append(to).append(" and model.isRead = 0");
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = messageDAO.findPage(page, hql.toString());
		return page;
	}
	
	/**
	 * 得到一条消息 
	 * @param id
	 * @return
	 */
	public Message getMessage(Integer id){
		return messageDAO.get(id);
	}
	
	/**
	 * 删除一条消息 
	 * @param msgId
	 */
	public void delMessage(Integer msgId){
		messageDAO.delete(msgId);
	}
	
	/**
	 * 更新一条消息
	 * @param message
	 */
	public void updateMessage(Message message){
		messageDAO.saveOrUpdate(message);
	}
}
